package mtn.bowen.yan.server.config;

import com.gn.decorator.GNDecorator;
import javafx.stage.Stage;
import mtn.bowen.yan.server.common.JsonResult;
import mtn.bowen.yan.server.fx.SpringFXMLLoader;
import mtn.bowen.yan.server.fx.StageManager;
import mtn.bowen.yan.server.enums.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.io.IOException;
import java.util.ResourceBundle;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: TODO
 * @date 2021-10-21 11:52 PM
 */
@Configuration
public class AppJavaConfig {
    @Autowired
    SpringFXMLLoader springFXMLLoader;

    /**
     * Useful when dumping stack trace to a string for logging.
     *
     * @return ExceptionWriter contains logging utility methods
     */
    public JsonResult<Object> exceptionWriter() {
        return new JsonResult<>(ErrorCode.SYSTEM_EXCEPTION, null);
    }

    @Bean
    public ResourceBundle resourceBundle() {
        return ResourceBundle.getBundle("Bundle");
    }

    @Bean
    @Lazy(value = true) //Stage only created after Spring context bootstap
    public StageManager stageManager(Stage stage, GNDecorator decorator) throws IOException {
        return new StageManager(springFXMLLoader, stage, decorator);
    }
}
